<?php

namespace Buu\Modules\Constant\Controllers;

use App\Models\ConstantVersion;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;
use Exception;
use Hhz\Drone\Gateway;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;
use App\Http\Controllers\Controller;

class ConstantVersionController extends Controller
{
    public function index(Request $request, Content $content)
    {
        $request = $request->all();
        return $content
            ->header('发布版本')
            ->breadcrumb(
                ['text' => '常量列表', 'url' => '/Constant/constant-type/list'],
                ['text' => '信息完善']
            )
            ->body($this->addAction($request));
    }

    public function addAction($request)
    {
        $aVersion = ConstantVersion::getLastVersion();
        $iVersion = $aVersion['major_version'].'.'.$aVersion['child_version'].'.'.$aVersion['fix_version'];

        $form = new Form(new ConstantVersion());
        $form->html("<span style='color:red;'><h3>当前版本：v$iVersion</h3>不可以修改, 请仔细填写</span>");

        $form->text('major_version', '主版本号')->rules('required')->pattern('[1-9]+')->width('200px')
            ->help('主版本号。当功能模块有较大的变动，比如增加多个模块或者整体架构发生变化。此版本号由项目决定是否修改');
        $form->text('child_version', '子版本号')->rules('required')->pattern('[0-9+]')->width('200px')
            ->help('子版本号。当功能有一定的增加或变化，比如增加了对权限控制、增加自定义视图等功能。此版本号由项目决定是否修改。');
        $form->text('fix_version', '修订版本')->rules('required')->pattern('[0-9+]')->width('200px')->rules('required')
            ->help('修订版本号。一般是 Bug 修复或是一些小的变动，要经常发布修订版，时间间隔不限，修复一个严重的bug即可发布一个修订版。此版本号由项目经理决定是否修改。');
        $form->textarea('desc', '说明')->width('300px');
        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();

            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();

            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();

        });
        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();
        });

        $form->confirm('确定 【提交 构建】 吗？');

        $form->setAction('/admin/Constant/constant-version/addsave');
        return $form;
    }

    public function addsave(Request $request)
    {
        $request = $request->all();
        $aVersion = ConstantVersion::getLastVersion();
        if($request['major_version'] < $aVersion['major_version']){
            $sMsg = '主版本号 不符合要求';
        }elseif($request['major_version'] == $aVersion['major_version']) {
            if ($request['child_version'] < $aVersion['child_version']) {
                $sMsg = '子版本号 不符合要求';
            } elseif ($request['child_version'] == $aVersion['child_version']) {
                if ($request['fix_version'] <= $aVersion['fix_version']) {
                    $sMsg = '修订版本号 不符合要求';
                }
            }
        }

        if(!empty($sMsg)){
            admin_toastr($sMsg, 'error', ['timeOut' => 10000]);
            return redirect('/admin/Constant/constant-version')->withInput();
        }
        $aValue = [
            'major_version' => $request['major_version'],
            'child_version' => $request['child_version'],
            'fix_version'   => $request['fix_version'],
            'desc'          => $request['desc'],
            'admin_id'      => Admin::user()->id,
            'addtime'       => time(),
        ];
        try {
            ConstantVersion::insert($aValue);
            $target = 'v'.$request['major_version'].'.'.$request['child_version'].'.'.$request['fix_version'];
            $this->drone($target);

            $sMsg = 'success';
        } catch (Exception $e) {
            print $e;
            $sMsg = 'error:'.$e;

        }

        admin_toastr($sMsg, $sMsg, ['timeOut' => 3]);
        return redirect('/admin/Constant/constant-version');
    }

    public function drone(string $target): void
    {
        $drone = new Gateway(
            'https://drone.haohaozhu.me/api/',
            'nFIBGyqDaNe0N4iS33ir9SAPg54nVAQ3'
        );
        $drone->Promote('docker/constant-gen', $target);
    }
}